package com.xexun.gpstracker.study;

import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

class DownFile {

    public static boolean getNetWorkFile(String urlString, String fileName) {
        URL url = null;
        try {
            url = new URL(urlString);
            // 使用HttpURLConnection打开连接
            HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
            urlConn.setDoInput(true);
            urlConn.connect();
            // 将得到的数据转化成InputStream
            InputStream is = urlConn.getInputStream();

            // 将InputStream转换成file
            FileOutputStream fos = null;

            File file = new File(fileName);
            if(!file.exists()){
                file.createNewFile();
            }
            fos = new FileOutputStream(file);
            // 将输入流is写入文件输出流fos中
            int ch = 0;
            try {
                while((ch=is.read()) != -1){
                    fos.write(ch);
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            } finally {
                //关闭输入流等
                fos.close();
                is.close();
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            System.out.println("[getNetWorkFile->]MalformedURLException");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("[getNetWorkFile->]IOException");
            e.printStackTrace();
        }
        return true;
    }
}